import QueryList from '@/components/basicWidget/QueryList'
import { updateDocumentById } from '@/service/document/documentService'
import { Switch } from 'antd'
import React, { Component } from 'react'

class UserManager extends Component {

    constructor(props) {
        super(props)
        this.state = {
        }
    }

    async componentDidMount() {

    }

    actionColumns = (columns, data) => {
        columns.push({ title: '管理员', dataIndex: 'adminController' })
        columns.push({ title: '超级管理员', dataIndex: 'superAdminController' })
        data = data.map(item => {
            item.adminController = (
                <Switch
                    checked={item.role === 'admin' || item.role === 'superAdmin'}
                    disabled={item.role === 'superAdmin'}
                    onChange={async (value) => {
                        const response = await updateDocumentById({
                            serviceName: 'blog',
                            collectionName: 'user_basic_info',
                            content: {
                                role: value ? 'admin' : 'normal'
                            },
                            id: item.id
                        })
                        if (response && response.code === 200) {
                            await this.queryList.onQuery()
                        }
                    }}
                >
                </Switch>
            )

            item.superAdminController = (
                <Switch
                    checked={item.role === 'superAdmin'}
                    disabled={true}
                >
                </Switch>
            )
            return item
        })
        return { newColumns: columns, newData: data }
    }

    render() {
        return (
            <>
                <QueryList
                    queryCode='userManager'
                    actionColumns={this.actionColumns}
                    ref={queryList => this.queryList = queryList}
                >
                </QueryList>
            </>
        )
    }
}

export default UserManager